<template>
  <div class="tp-box">
    <a-drawer
      :visible="props.visible"
      :width="props.width"
      :closable="props.closable"
      :destroyOnClose="props.destroyOnClose"
      @close="handleClose"
    >
      <template #title>
        <div class="title-detail">
          <Tooltip placement="top">
            <template #title>
              <span>{{ getTipTitle }}</span>
            </template>
            <h3 style="padding-top: 10px;">{{ getTitle }}</h3>
          </Tooltip>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <div style="width: 400px; padding-top: 5px">
            <a-input-search
              v-model:value="txtKeyWord"
              placeholder="请输入户主姓名、地址或房屋编号进行查询"
              enter-button
              style="width: 100%"
              @search="onSearch"
            />
          </div>
          <a-space style="float: right; margin-right: 10px;">
            <a-button type="primary" @click="handleHoustDetailExport"> 导出全部</a-button>
          </a-space> 
        </div>
      </template>

      <!-- :scroll="{ x: 1500, y: 600 }" -->
      <a-table
        :columns="computedTableColumn"
        :data-source="computedTableData"
        :page-size-options="pageSizeOptions"
        :loading="loading"
        bordered
        :pagination="false"
        size="middle"
        :scroll="{ x: 'calc(700px + 50%)', y: props.detailTableHeight }"
      >
        <!-- <template #action>
          <a>action</a>
        </template> -->
      </a-table>
      <div class="pagination">
        <a-pagination
          show-size-changer
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </a-drawer>
    <!-- <CheckDetail
      v-model:vvvisible="vvvisible"
      title="抽屉标题"
      :width="1500"
      :detailTableHeight="detailTableHeight"
      :vclosable="true"
      :vdestroyOnClose="true"
      :data="vvvdetailData"
      :vvvdetailParams="vvvdetailParams"
      :vvvcurrentTitle="vvvcurrentTitle"
      @close="handleVillageClose"
    >
    
    </CheckDetail> -->
  </div>
</template>
<script setup lang="ts">
  import { Tooltip } from 'ant-design-vue';
  import { ref, computed, onMounted, watch } from 'vue';
  import { useUserStore } from '/@/store/modules/user';
  import { villagePostStaticDetail } from '/@/api/demo/system';
  // import CheckDetail from '/@/views/village/threePure/cjworkstatic/component/checkDetailDrawer.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  // 表格的 ref Dom名称
  const vvvisible = ref(false);
  let vvvdetailData = ref<any[]>([]);
  let vvvdetailParams = ref({}); // 详情参数
  let detailTableHeight = ref<number>(0); // 表格高度
  const userStore = useUserStore();
  const globSetting = useGlobSetting();
  let vvvcurrentTitle = ref<string>();
  // 默认传过来的值q
  const props = defineProps({
    visible: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: '',
    },
    width: {
      type: Number,
      default: 256,
    },
    detailTableHeight: {
      type: Number,
      default: 600,
    },
    hzfs: {
      type: String,
      default: '',
    },
    closable: {
      type: Boolean,
      default: true,
    },
    data: {
      type: Array,
      default: () => [],
    },
    detailParams: {
      type: Object,
      default: () => {},
    },
    currentXzqdmname: {
      type: String,
      default: '',
    },
    currentTitle: {
      type: String,
      default: '',
    },
    wscolsList: {
      type: Array,
      default: () => [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
    },
    destroyOnClose: {
      type: Boolean,
      default: true,
    },
    onClose: {
      type: Function as PropType<() => void>,
      default: () => {},
    },
  });
  let pageIndex = ref(1);
  let pageSize = ref(15);
  let loading = ref(false);
  const pageSizeOptions = ref<string[]>(['100', '200', '300']);
  let tableSource = ref<any>([]);
  let total = ref(0);
  const columns = [
    { title: '序号', width: 50, dataIndex: 'index', key: 'index', align: 'center', fixed: 'left' },
    {
      title: '区县名称',
      dataIndex: 'xzq',
      key: 'xzq',
      align: 'center',
      width: 90,
    },
    {
      title: '乡镇名称',
      dataIndex: 'town',
      key: 'town',
      align: 'center',
      width: 100,
    },
    {
      title: '村级名称',
      dataIndex: 'village',
      key: 'village',
      align: 'center',
      width: 150,
    },
    {
      title: '户主姓名',
      dataIndex: 'hzxm',
      key: 'hzxm',
      align: 'left',
      width: 210,
    },
    {
      title: '地址',
      dataIndex: 'address',
      key: 'zcl',
      align: 'center',
      width: 300,
    },
    {
      title: '房屋编号',
      dataIndex: 'fwbh',
      key: 'fwbh',
      align: 'center',
      width: 150,
      customCell: (row, index, column) => {
        let isNoHzyh = row.town !== '汇总' ? true : false;
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = props.detailParams.category;
        return {
          style: `${isNoHzyh === true && isNoZero != 2 ? style : ''}`,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData(row, index, column, isNoHzyh, isNoZero);
          },
        };
      },
    },
  ];

  // 计算总数的方法
  function vtotal(arr: any, item: any) {
    let total = arr.reduce((pre: any, cur: any) => {
      return parseInt(pre) + parseInt(cur[item]);
    }, 0);
    return total;
  }
  // 计算属性，用于实时更新数据到页面上
  const computedTableColumn = computed(() => {
    let newColumn: any = [];
    newColumn = columns;
    return newColumn;
  });
  const computedTableData = computed(() => {
    console.log(tableSource.value);
    return tableSource.value;
  });
  const getTitle = computed(() => {
    // let str = `【${props.currentXzqdmname} 】-- ${props.currentTitle}`;
    console.log('******参数******',);
    let str = `【${props.currentTitle}】的`+ props.detailParams.titlename;
    if (str.length > 50) {
      str = str.substring(0, 50) + '...';
    }
    return str;
  });
  const getTipTitle = computed(() => {
    let str = `${props.currentTitle}`;
    return str;
  });
  watch(
    () => props.visible,
    (newVal, oldVal) => {
      // console.log(`visible 变化了，新值：${newVal}，旧值：${oldVal}`);
      // 在这里执行你需要的操作
      if (newVal) {
        getTableData();
      }
    },
  );
  const getTableData = async () => {
    let params: any = {
      pageIndex: pageIndex.value,
      pageSize: pageSize.value,
      ...props.detailParams,
      txtKeyWord: txtKeyWord.value,
    };
    console.log('*********************', params);

    loading.value = true;
    let res = await villagePostStaticDetail(params);
    loading.value = false;
    let list = res.items;
    tableSource.value = list;
    total.value = res.total;

    // console.log('@resqqq', list);
  };
  //  调用 父组件传过来的方法
  const emit = defineEmits(['close']);
  // 导出
  const getDetailData = async (
    row: any,
    index: number,
    column: any,
    isNoHzyh: boolean,
    isNoZero: number,
  ) => {
    if (isNoHzyh && isNoZero != 2) {
      //除未自查外
      let params: any = {
        town: row.town,
        fwbh: row.fwbh,
        ...props.detailParams,
      };
      vvvdetailParams.value = params;
      vvvcurrentTitle.value = row.xzq + '-' + row.town + '-' + row.village + '【' + row.fwbh + '】';
      showDeatailDrawer();
    }
  };

  // 打开抽屉
  function showDeatailDrawer() {
    vvvisible.value = true;
  }
  const txtKeyWord = ref<string>('');

  //  搜索事件
  const onSearch = () => {
    getTableData();
  };
 
  function handleHoustDetailExport() {
    console.log('下载')
    const uriBase = `${globSetting.apiBaseUrl}${globSetting.apiUrl}`;
    const tk = userStore.getToken;
    downloadByUrl({
      url:
        uriBase +
        '/houses/evolvestatic/ExportHouseDetailedExcel?access_token=' +
        tk +
        '&startTime=' +
        props.detailParams.startTime +
        '&endTime=' +
        props.detailParams.endTime +
        '&xzqdm=' +
        props.detailParams.xzqdm +
        '&towndm=' +
        props.detailParams.towndm +
        '&villagedm=' +
        props.detailParams.villagedm +
        '&category=' +
        props.detailParams.category,

        target: '_blank',
     });
  }

  const handleClose = () => {
    emit('close');
    pageIndex.value = 1;
    pageSize.value = 15;
    vvvisible.value = false;
  };
  const handleVillageClose = () => {
    vvvisible.value = false;
  };
  // 切换页码的时候更新  pageIndex 和  pageSize
  const onShowSizeChange = (pageIndex: number, pageSize: number) => {
    // antDesign  自动给  pageIndex 和 pageSize 赋值
    // pageIndex.value = pageIndex;
    // pageSize.value = pageSize;
    // console.log('@pageIndex,pageSize', pageIndex, pageSize);
  };
  watch(pageSize, () => {
    // console.log('pageSize', pageSize.value);
    getTableData();
  });
  watch(pageIndex, () => {
    // console.log('current', pageIndex.value);
    getTableData();
  });
  onMounted(() => {
    getTableData();
  });
</script>
<style lang="less" scoped>
  .tp-box {
    height: 100%;
  }
  .title-detail {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .pagination {
    margin-top: 5px;
    display: flex;
    justify-content: flex-end;
  }

</style>
